<?php
    namespace lib\core;

    class load{
        
        //PSR4
        private static $psr4Name = [];
        
        //PSR0
        private static $psr0Name = [];
        
        /**
         * 自动加载
         */
        public static function autoload($className){
            
            $file = self::paramPsr0($className);
	        $file = __DIR__."/../../".$file;
            if(file_exists($file)){
                self::__include($file);
            }else{
                throw new \Exception('class not found :'.$file);
            }
        }
        
        /**
         * 查找文件
         */
        private static function findFile($class){
            $psr4 = self::$psr4namespace;
            if($psr4){
                foreach($psr4 as $key=>$path){
                    $filename = $path.'/'.substr(strrchr($class,'\\'),1).EXT;
                    if(file_exists($filename)){
                        return $filename;
                    }
                }
            }
        }
        
        /**
         * 添加psr4规范
         */
        private static function addPsr4(array $param){}
        
        /**
         * 添加psr0规范
         */
        private static function addPsr0(){}
        
        /**
         * 解析psr0规范
         */
        private static function paramPsr0($className = ""){
            
            $fileName  = '';
            $namespace = '';
            if ($lastNsPos = strrpos($className, '\\')) {
                $namespace = substr($className, 0, $lastNsPos);
                $className = substr($className, $lastNsPos + 1);
                $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
            }
            
            $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
            return $fileName;
        }
        
        /**
         * 注册
         */
        public static function register(){
            spl_autoload_register('lib\core\load::autoload');
        }
        
        /**
         * 封装引入
         */
        private static function __include($file){
            include $file;
        }
        
        private static function __require($file){
            require $file;
        }
    }
